package com.li.o2oSystem.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.li.o2oSystem.cache.JedisUtil;
import com.li.o2oSystem.dao.ShopCategoryDao;
import com.li.o2oSystem.entity.ShopCategory;
import com.li.o2oSystem.exceptions.ShopCategoryOperationException;
import com.li.o2oSystem.exceptions.ShopOperationException;
import com.li.o2oSystem.service.ShopCategoryService;
@Service
public class ShopCategoryServiceImpl implements ShopCategoryService{
	@Autowired
	private ShopCategoryDao shopCategoryDao;
	@Autowired
	private JedisUtil.Keys jedisKeys;
	@Autowired
	private JedisUtil.Strings jedisStrings;
	
	private Logger logger = LoggerFactory.getLogger(ShopCategoryServiceImpl.class);
	/**
	 * 获取店铺类别信息
	 */
	public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) {
		//定义key前缀
		String key = SCLISTKEY;
		//定义接收数据的对象
		List<ShopCategory> shopCategoryList = null;
		//定义jackson对象
		ObjectMapper mapper = new ObjectMapper();
		//拼接出redis的key
		if(shopCategoryCondition == null){
			//如果条件为空，则列出所有大类
			key = key + "_allfirstlevel";
		}else if (shopCategoryCondition != null && shopCategoryCondition.getParent() != null && shopCategoryCondition.getParent().getShopCategoryId() != null) {
			//如果条件不为空，且父类不为空，且父类id不为空
			key = key + "_parent" + shopCategoryCondition.getParent().getShopCategoryId();
		}else if(shopCategoryCondition != null){
			//如果条件不为空，则都列出来
			key = key + "_allsecondlevel";
		}
		
		//redis判断key是否存在
		if(!jedisKeys.exists(key)){
			//如果不存在则，从数据库中获取数据
			shopCategoryList =  shopCategoryDao.queryShopCategory(shopCategoryCondition);
			//将获取的数据装换为字符串
			String jsonString;
			try {
				jsonString = mapper.writeValueAsString(shopCategoryList);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
				logger.error("将店铺类别转换为json字符串发生异常。");
				throw new ShopCategoryOperationException(e.getMessage());
			}
			//将key和json字符串存入到redis中
			jedisStrings.set(key, jsonString);
		}else{
			//如果已经存在redis中，根据key取出
			String jsonString = jedisStrings.get(key);
			//获取要装换的类型
			JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, ShopCategory.class);
			//将json字符串装换为Java对象
			try {
				shopCategoryList = mapper.readValue(jsonString, javaType);
			} catch (JsonParseException e) {
				e.printStackTrace();
				throw new ShopCategoryOperationException(e.getMessage());
			} catch (JsonMappingException e) {
				e.printStackTrace();
				throw new ShopCategoryOperationException(e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				throw new ShopCategoryOperationException(e.getMessage());
			}
		}
		return shopCategoryList;
	}
}
/**
 * ①定义将要存入redis中的key的前缀(作为final字段，定义在接口中)
 * ②定义接收头条列表的对象
 * ③定义jackson对象，对数据进行装换
 * ④拼接根据条件，将要存入redis中的的key，分开存储
 * ⑤先判断redis中是否存在key和对应的内容
 * ⑥如果不存在，直接从数据看中获取数据，并将对象数据庄换为json字符串，将key对应字符串存储到redis中
 * ⑦如果存在，则从redis中根据key取出对应的字符串，并通过定义需要转换的类型，将json字符串装换为对应的Java对象
 * ⑧返回数据
 * */